package pl.edu.pw.ee.buildcity.scm;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.tmatesoft.svn.core.internal.io.dav.DAVRepositoryFactory;
import org.tmatesoft.svn.core.internal.io.svn.SVNRepositoryFactoryImpl;
import pl.edu.pw.ee.buildcity.scm.subversion.SubversionServiceImpl;

/**
 * @author mgrzechocinski
 * @since 2009-09-19
 */
public enum SCMConnector {

	SVN {
		@Override
		public String getProtocol() {
			return "svn";
		}

		@Override
		public void setup() {
			SVNRepositoryFactoryImpl.setup();
		}

		/**
		 * @see <a href="http://en.wikipedia.org/wiki/Subversion_%28software%29">'Custom "svn" protocol (default port 3690) [...]'</a>
		 */
		@Override
		public int getPort() {
			return 3690;
		}

	},

	HTTP {
		@Override
		public String getProtocol() {
			return "http";
		}

		@Override
		public void setup() {
			DAVRepositoryFactory.setup();
		}

		@Override
		public int getPort() {
			return 80;
		}

	};

	private static final Logger log = Logger.getLogger(SCMConnector.class);

	public static SCMConnector getByProtocol(String protocol){
			for (SCMConnector connector : values()) {
				if(StringUtils.equalsIgnoreCase(protocol, connector.getProtocol())){
					return connector;
				}
			}
			log.warn("Could not find proper SCMConnector for protocol: '" + protocol + "'");
			return null;
	}	

	public abstract String getProtocol();

	public abstract void setup();

	public abstract int getPort();
}
